package cn.edu.sptc.sms_chensiyi.controller;

import cn.edu.sptc.sms_chensiyi.dao.MajorMapper;
import cn.edu.sptc.sms_chensiyi.entity.Major;
import cn.edu.sptc.sms_chensiyi.service.MajorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//控制层
@RestController
@RequestMapping("/major")
public class MajorController {
    @Autowired
    private MajorService majorService;


    //select
//    @GetMapping("/query/{id}")
//    public Major queryMajorByID(@PathVariable String id){
//        return majorService.queryByID(Long.parseLong(id));//string转换为long型
//    }

    @GetMapping("/query/{id}")
    public Map<String,Object> queryMajorByID(@PathVariable String id){
        Map map=new HashMap();
        map.put("code","200");
        map.put("msg","成功");
        map.put("data",majorService.queryByID(Long.parseLong(id)));
        return map;//string转换为long型
    }

    @GetMapping("/query")
    public Map<String,Object> queryMajorByIDParam(@RequestParam String id){
        Map map=new HashMap();
        map.put("code","200");
        map.put("msg","成功");
        map.put("data",majorService.queryByID(Long.parseLong(id)));
        return map;

    }

    @GetMapping("/queryAll")
    public Map<String,Object> queryMajorByall (){
        Map map=new HashMap();
        map.put("code","200");
        map.put("msg","成功");
        map.put("data",majorService.queryByALl());
        return map;
    }


    //insert
//    @GetMapping("/save")
//    public Map<String,Object> insertMajor(@RequestBody Major major){
//        Map map=new HashMap();
//        if (majorService.insertMajor(major)){
//            map.put("code","200");
//            map.put("msg","成功");
//        }else {
//            map.put("code","201");
//            map.put("msg","失败");
//        }
//        return map;
//    }

    @PostMapping(value = "/save",consumes = MediaType.APPLICATION_JSON_VALUE)
    public Map<String,Object> insertByJSON(@RequestBody Major major){
        Map map=new HashMap<>();
        major.setCreateTime(new Date());
        major.setUpdateTime(new Date());
        if(majorService.insertMajor(major)){
            map.put("code","200");
            map.put("msg","成功");
        }else {
            map.put("code","201");
            map.put("msg","失败");
        }
        return map;
    }

    @PostMapping(value = "/save",consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public Map<String,Object> insertByFROM(Major major){
        Map map=new HashMap<>();
        major.setCreateTime(new Date());
        major.setUpdateTime(new Date());
        if(majorService.insertMajor(major)){
            map.put("code","200");
            map.put("msg","成功");
        }else {
            map.put("code","201");
            map.put("msg","失败");
        }
        return map;
    }

    @PostMapping(value = "/update",consumes = MediaType.APPLICATION_JSON_VALUE)
    public Map<String,Object> updateByJSON(@RequestBody Major major){
        Map map=new HashMap<>();
        major.setUpdateTime(new Date());
        if(majorService.updateMajor(major)){
            map.put("code","200");
            map.put("msg","成功");
        }else {
            map.put("code","201");
            map.put("msg","失败");
        }
        return map;
        }

    @PostMapping(value = "/update",consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public Map<String,Object> updateByFROM(Major major){
        Map map=new HashMap<>();
        major.setUpdateTime(new Date());
        if(majorService.updateMajor(major)){
            map.put("code","200");
            map.put("msg","成功");
        }else {
            map.put("code","201");
            map.put("msg","失败");
        }
        return map;
    }

    @GetMapping("/delete/{id}")
    public Map<String,Object> deleteMajorByID(@PathVariable String id){
        Map map=new HashMap();
        if(majorService.deleteMAjor(Long.parseLong(id))){
            map.put("code","200");
            map.put("msg","成功");
        }else {
            map.put("code","201");
            map.put("msg","失败");
        }
        return map;//string转换为long型
    }

    @GetMapping("/delete")
    public Map<String,Object> deleteMajorByParam(@RequestParam String id){
        Map map=new HashMap();
        if(majorService.deleteMAjor(Long.parseLong(id))){
            map.put("code","200");
            map.put("msg","成功");
        }else {
            map.put("code","201");
            map.put("msg","失败");
        }
        return map;//string转换为long型
    }

}
